Robot = function(x, y, heading, speed, isParticle) {	
	this.isParticle = isParticle;
	this.angle = heading;
	this.x = x;
	this.y = y;
	this.speed = speed || 0;
	this.collision = false;
	this.priority = 10;
	this.points = [[0, -13], [-7, 7], [7, 7]];	
	this.poly = [];
	
	this.refreshPoly = function() {
		for (n=0; n<this.points.length; n++) {
			this.poly[n] = 
				[this.points[n][0] * Math.cos(this.angle) - this.points[n][1] * Math.sin(this.angle) + this.x, 
				this.points[n][0] * Math.sin(this.angle) + this.points[n][1] * Math.cos(this.angle) + this.y];
		}
	}
	
	this.refreshPoly();
	
	this.get_collision_poly = function() {
		return this.poly;
	}
	
	this.keyHeld_37 = this.keyDown_37 = function () {
		this.angle -= 0.1;
	}
	
	this.keyHeld_39 = this.keyDown_39 = function () {
		this.angle += 0.1;
	}
	
	this.keyDown_38 = function () {
		if (this.speed < 1) {
			this.speed = 1;
		}
	}
	
	this.keyHeld_38 = function () {
		if (this.speed < 5.0)
			this.speed += 0.3;
	}
	
	this.update = function() {
		if (this.speed > 0 && this.isParticle) {
			this.speed += noise(MOVE_NOISE);
			this.angle += noise(TURN_NOISE);
		}
		
		if (this.speed > 0.1) {
			this.speed -= 0.1;
		} else {
			this.speed = 0;
		}
					
		var deltaX = this.speed * (Math.sin(this.angle));
		var deltaY = -this.speed * (Math.cos(this.angle));
		this.x += deltaX;
		this.y += deltaY;
		
		this.refreshPoly();
		
		collide.polys([this], WALLS);
		if (this.collision) {
			this.x -= deltaX;
			this.y -= deltaY;
			this.speed = 0;
			this.collision = false;
			this.refreshPoly();
		}
	}
	
	this.collide_poly = function() {
		this.collision = true;
	}
	
    this.draw = function(c, gs) {
		gs.polygon(this.poly, false, true);
    }
	
	this.getMeasurement= function() {
		return new Measurement(this);
	}
}

function noise(stdDev) {
	return ((Math.random()*2-1)+(Math.random()*2-1)+(Math.random()*2-1))*stdDev;
}